package com.hongmeng.operater.service.newsAndVideo.impl;

import java.util.Date;
import java.util.List;

import com.hongmeng.common.constant.Constants;
import com.hongmeng.common.core.domain.AjaxResult;
import com.hongmeng.common.utils.DateUtils;
import com.hongmeng.common.utils.MessageUtils;
import com.hongmeng.common.utils.SecurityUtils;
import com.hongmeng.framework.web.service.PermissionService;
import com.hongmeng.operater.domain.HmFile;
import com.hongmeng.operater.domain.newsAndVideo.entity.NewsInfoEntity;
import com.hongmeng.operater.mapper.HmFileMapper;
import com.hongmeng.operater.mapper.newsAndVideo.HmNewsExtMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hongmeng.operater.mapper.newsAndVideo.HmNewsMapper;
import com.hongmeng.operater.domain.newsAndVideo.HmNews;
import com.hongmeng.operater.service.newsAndVideo.IHmNewsService;

/**
 * 文章信息Service业务层处理
 *
 * @author hongmeng
 * @date 2020-12-21
 */
@Service
@Slf4j
public class HmNewsServiceImpl implements IHmNewsService {

    @Autowired
    private HmNewsMapper hmNewsMapper;

    @Autowired
    private HmNewsExtMapper hmNewsExtMapper;

    @Autowired
    private HmFileMapper hmFileMapper;

    @Autowired
    private PermissionService permissionService;

    /**
     * 查询文章信息
     *
     * @param id 文章信息ID
     * @return 文章信息
     */
    @Override
    public HmNews selectHmNewsById(Integer id) {
        return hmNewsMapper.selectHmNewsById(id);
    }

    /**
     * 查询文章信息列表
     *
     * @param hmNews 文章信息
     * @return 文章信息
     */
    @Override
    public List<NewsInfoEntity> selectHmNewsList(HmNews hmNews) {
        //如果是运营管理，则只显示待审核信息（定时提交的只能看到当前时间之前的信息）
        if (permissionService.hasRole(Constants.ROLE_KEY.OP_ADMIN)&&
                !permissionService.hasRole(Constants.ROLE_KEY.ADMIN)){
            return hmNewsExtMapper.selectHmNewsListByAdmin(hmNews);
        }
        //否，则显示全部信息
        return hmNewsExtMapper.selectHmNewsList(hmNews);
    }

    /**
     * 新增文章信息
     *
     * @param hmNews 文章信息
     * @return 结果
     */
    @Override
    public AjaxResult insertHmNews(HmNews hmNews) {
        //新增文章状态必须为定时提交，待审核 或 已提交，待审核。
        if (!Constants.NEWS_AND_VIDEO_STATE.TIMING_SUBMIT.equals(hmNews.getNewsState())&&
                !Constants.NEWS_AND_VIDEO_STATE.SUBMIT.equals(hmNews.getNewsState())) {
            log.info(MessageUtils.message("no.create.newsState"));
            return AjaxResult.error(MessageUtils.message("no.create.newsState"));
        }
        HmFile file = hmFileMapper.selectHmFileById(hmNews.getThumbnailId());
        if (file==null){
            log.info("缩略图不能为空");
            return AjaxResult.error(MessageUtils.message("no.null.thumbnailId"));
        }
        hmNews.setCreateTime(DateUtils.getNowDate());
        hmNews.setCreateBy(SecurityUtils.getUsername());
        //初始阅读量/浏览量为0
        hmNews.setViewCount(0);
        return AjaxResult.success(hmNewsMapper.insertHmNews(hmNews));
    }

    /**
     * 修改文章信息
     *
     * @param hmNews 文章信息
     * @return 结果
     */
    @Override
    public int updateHmNews(HmNews hmNews) {
        hmNews.setUpdateTime(DateUtils.getNowDate());
        hmNews.setUpdateBy(SecurityUtils.getUsername());
        //文章修改后自动变更状态为已提交，待审核
        hmNews.setNewsState(Constants.NEWS_AND_VIDEO_STATE.SUBMIT);
        return hmNewsMapper.updateHmNews(hmNews);
    }

    /**
     * 批量删除文章信息
     *
     * @param ids 需要删除的文章信息ID
     * @return 结果
     */
    @Override
    public int deleteHmNewsByIds(Integer[] ids) {
        return hmNewsMapper.deleteHmNewsByIds(ids);
    }

    /**
     * 删除文章信息信息
     *
     * @param id 文章信息ID
     * @return 结果
     */
    @Override
    public int deleteHmNewsById(Integer id) {
        return hmNewsMapper.deleteHmNewsById(id);
    }

    /**
     * 审核文章信息
     *
     * @param id      文章信息ID
     * @param yesOrNo 文章审核是否通过
     * @param newsRemarks 审核备注(通过与不通过的理由)
     * @return 结果
     */
    @Override
    public AjaxResult examine(Integer id, boolean yesOrNo, String newsRemarks) {
        //只有拥有运营管理权限才能审核信息
        if (!permissionService.hasRole(Constants.ROLE_KEY.OP_ADMIN)){
            log.info("只有运营管理才能审核信息");
            return AjaxResult.error(MessageUtils.message("not.newsAdmin"));
        }
        HmNews hmNews = hmNewsMapper.selectHmNewsById(id);
        if (hmNews == null) {
            log.info("审核文章失败，没有id为" + id + "的文章");
            return AjaxResult.error(MessageUtils.message("no.newsId"));
        }
        if (!Constants.NEWS_AND_VIDEO_STATE.SUBMIT.equals(hmNews.getNewsState()) &&
                !Constants.NEWS_AND_VIDEO_STATE.TIMING_SUBMIT.equals(hmNews.getNewsState())){
            log.info("当前文章状态不正确无法审核");
            return AjaxResult.error(MessageUtils.message("not.state"));
        }
        if (Constants.NEWS_AND_VIDEO_STATE.TIMING_SUBMIT.equals(hmNews.getNewsState())){
            Date nowDate = DateUtils.getNowDate();
            if (hmNews.getPubdate().compareTo(nowDate)>0){
                log.info("当前文章状态不正确无法审核");
                return AjaxResult.error(MessageUtils.message("not.state"));
            }
        }
        if (yesOrNo) {
            hmNews.setNewsState(Constants.NEWS_AND_VIDEO_STATE.PASS_AND_RELEASE);
            hmNews.setPubdate(DateUtils.getNowDate());
        } else {
            hmNews.setNewsState(Constants.NEWS_AND_VIDEO_STATE.NOT_PASS);
        }
        hmNews.setNewsRemarks(newsRemarks);
        int count = hmNewsMapper.updateHmNews(hmNews);
        log.info("审核文章信息共{}条", count);
        return AjaxResult.success("文章审核成功");
    }

    /**
     * 获取文章信息详细信息(新增一个阅读量/浏览量)
     *
     * @param id 文章id
     * @return 结果
     */
    @Override
    public AjaxResult getNewsInfo(Integer id) {
        HmNews hmNews = hmNewsMapper.selectHmNewsById(id);
        if (hmNews == null) {
            log.info("没有id为" + id + "的文章");
            return AjaxResult.error(MessageUtils.message("no.newsId",id));
        }
        hmNews.setViewCount(hmNews.getViewCount() + 1);
        int count = hmNewsMapper.updateHmNews(hmNews);
        log.info("文章新增一个阅读量/浏览量共{}个", count);
        return AjaxResult.success(hmNews);
    }

    @Override
    public AjaxResult getAdjacentNews(Integer id, String type) {
        HmNews hmNews = hmNewsMapper.selectHmNewsById(id);
        if (hmNews==null){
            log.info("文章信息id:{}不存在",id);
            return AjaxResult.error(MessageUtils.message("no.newsId",id));
        }
        NewsInfoEntity newsInfo ;
        if (Constants.GET_NEWS_TYPE.UP.equals(type)){
            newsInfo = hmNewsExtMapper.getPreviousNews(id);
        }else {
            newsInfo = hmNewsExtMapper.getNextNews(id);
        }
        return AjaxResult.success(newsInfo);
    }
}
